package com.smbe.netty.protobuf;

import com.smbe.pb.demo.DateInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Date;
import java.util.Random;

/**
 * <p>Title: </p>
 * <p>Company:misspao </p>
 *
 * @author: buwenmin
 * @Date: Create in 23:38 2019/2/19
 * @Description:
 */
public class MyClientHandler extends SimpleChannelInboundHandler<DateInfo.Message> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DateInfo.Message msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        int random = new Random().nextInt(3);
        DateInfo.Message build = null;

        if(0==random){
             build = DateInfo.Message.newBuilder().setDataType(DateInfo.Message.DataType.PersonType).
                    setPerson(DateInfo.Person.newBuilder().setAddress("北京").setAge(24).setName("smbe").build())
                    .build();
        }else if (1==random){
            build = DateInfo.Message.newBuilder().setDataType(DateInfo.Message.DataType.DogType).
                    setDog(DateInfo.Dog.newBuilder().setAge(24).setName("snoopy").build())
                    .build();

        }else{
            build = DateInfo.Message.newBuilder().setDataType(DateInfo.Message.DataType.CatType).
                    setCat(DateInfo.Cat.newBuilder().setAddress("北京").setName("tom").build())
                    .build();
        }

        ctx.writeAndFlush(build);
    }
}
